package com.xxl.job.admin.core.util;

import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenlinqing
 * @description:
 * @date: 2025/8/8 15:02
 */
public class LogXssTagUtil {
    /**
     * filter xss tag
     *
     * @param originData
     * @return
     */
    public static String filter(String originData){

        // exclude tag
        Map<String, String> excludeTagMap = new HashMap<String, String>();
        excludeTagMap.put("<br>", "###TAG_BR###");
        excludeTagMap.put("<b>", "###TAG_BOLD###");
        excludeTagMap.put("</b>", "###TAG_BOLD_END###");

        // replace
        for (String key : excludeTagMap.keySet()) {
            String value = excludeTagMap.get(key);
            originData = originData.replaceAll(key, value);
        }

        // htmlEscape
        originData = HtmlUtils.htmlEscape(originData, "UTF-8");

        // replace back
        for (String key : excludeTagMap.keySet()) {
            String value = excludeTagMap.get(key);
            originData = originData.replaceAll(value, key);
        }

        return originData;
    }
}
